import vue from "@vitejs/plugin-vue";
import type { UserConfig } from 'vite';
import { resolve } from 'path'
import { loadEnv } from './src/utils/viteBuild'

const { VITE_PORT, VITE_OPEN, VITE_PUBLIC_PATH } = loadEnv();

const pathResolve = (dir: string): any => {
	return resolve(__dirname, '.', dir);
};
const alias: Record<string, string> = {//还需要去tsconfig中配置
	'@': pathResolve('/src/'),
	'/@': pathResolve('/src/'),
	'@assets': pathResolve('/src/assets'),
	'@components': pathResolve('/src/components'),
	'@views': pathResolve('/src/views'),
	'@layout': pathResolve('/src/layout'),
	'@https': pathResolve('/src/https'),
	'@hooks': pathResolve('/src/hooks'),
	'@utils': pathResolve('/src/utils'),
	'@store': pathResolve('/src/store'),
	'@router': pathResolve('/src/router'),
	'@theme': pathResolve('/src/theme'),
	//'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
};

const viteConfig: UserConfig = {
  plugins: [vue()],
  base: process.env.NODE_ENV === 'production' ? VITE_PUBLIC_PATH : './',
  resolve: { alias },
  build: {
    outDir: "vue-web",
  },
  server: {
    https: false, // 是否开启 https
    open: VITE_OPEN, // 是否自动在浏览器打开
    port: VITE_PORT, // 端口号
    host: "0.0.0.0",
    proxy: {
      "/api": {
        target: "", // 后台接口
        changeOrigin: true,
        secure: false, // 如果是https接口，需要配置这个参数
        // ws: true, //websocket支持
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  // 引入第三方的配置
  optimizeDeps: {
		include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw'],
  },
};

export default viteConfig;